﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using STHPMS1.Common.C_Extension;
using STHPMS1.Common.C_Utils;
using STHPMS1.ICore.I_Serv;
using STHPMS1.BLL.B_Serv;
using STHPMS1.Common.C_Enum;
using System.Text;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/modelgroup")]
    public class Dm_ModelgroupPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetmodelgroupByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(int? index = null, int? pvmt = null, int? basic = null, int? model = null, int? type = null, bool? check=null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" where 1=1 ");
            if (index != null)
            {
                sb.AppendFormat(" and a.Indextype_Dictid = {0} ",index);
            }

            if (pvmt != null)
            {
                sb.AppendFormat(" and a.Pvmttype_Dictid = {0} ", pvmt);
            }


            if (basic != null)
            {
                sb.AppendFormat(" and a.Basictype_Dictid = {0} ", basic);
            }


            if (model != null)
            {
                sb.AppendFormat(" and a.Modelmode_Dictid = {0} ", model);
            }

            if (type != null)
            {
                sb.AppendFormat(" and a.Modeltype_Dictid = {0} ", type);
            }

            IScene scene = this.Scene;
            var data = scene.Bll.Dm_Modelgroup.GetAllModelGroups<Dm_Modelgroup>(sb.ToString(), info, check);
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

      

        [Route("")]
        public HttpResponseMessage Post(Dm_Modelgroup entity)
        {
            this.Scene.Bll.Dm_Modelgroup.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetmodelgroupByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("")]
        public void Delete(Dm_Modelgroup entity)
        {
            this.Scene.Bll.Dm_Modelgroup.Remove(entity);
        }

        [Route("show")]
        public dynamic GetModelDetail(string modelId)
        {
            return this.Scene.Bll.Dm_Modelgroup.GetModelDetail(modelId);
        }

        [Route("calc")]
        public void GetCalcReturn(string modelId, int modelMode, int beginYear, int endYear)
        {
            this.Scene.Bll.Dm_Modelgroup.CalcReturn(modelId,modelMode,beginYear,endYear);
        }

        [Route("graphic")]
        public dynamic GetGraphic(string modelId)
        {
            var series = this.Scene.Bll.Dm_Modelgroup.GetModelGraphic(modelId);

            Dm_Modelgroup model = this.Scene.Dal.Sthpms1.SingleById<Dm_Modelgroup>(modelId);
            IModelCalculateService modelServ = ModelCalculateService.CreateServiceByModel(model); 
            string formula = modelServ.PrintForumla();

            var outData = new
            {
                credits = new
                {
                    enabled = false
                },
                title = new
                {
                    text = formula,
                    y = 100
                },
                xAxis = new
                {
                    title = new
                    {
                        text = "累计轴载量（百万次）"
                    },
                    min = 0
                },
                yAxis = new
                {
                    title = new
                    {
                        text = ((RegressionIndicatorsEnum)model.Indextype_Dictid).GetEnumDescription()
                    },
                    min = 0,
                    max=100
                },
                //legend = new
                //{
                //    layout = "vertical",
                //    align = "right",
                //    verticalAlign = "right",
                //    x = 0,
                //    y = 0,
                //    floating = true,
                //    borderWidth = 1
                //},
                series = series.ToArray(),
            };
            return outData;
        }
    }
}